package cn.hacynn.nd.config;

import cn.hacynn.nd.common.properties.OssProperties;
import cn.hacynn.nd.common.properties.STSProperties;
import cn.hacynn.nd.common.utils.OSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author Hacynn
 * @Date 2024/5/25 19:40
 * @Version 1.0
 * 配置类, 用于创建 OSSUtil 对象
 */
@Configuration
@Slf4j
public class OssConfiguration {

    @Autowired
    private STSProperties stsProperties;

    @Bean
    @ConditionalOnMissingBean  // 当没有该bean时才注册bean到IOC容器, 保证只有一个对象
    public OSSUtil ossUtil(OssProperties ossProperties) {
        log.info("开始创建阿里云文件上传工具类对象: {}", ossProperties);
        return new OSSUtil(ossProperties.getEndpoint(),
                ossProperties.getAccessKeyId(),
                ossProperties.getAccessKeySecret(),
                ossProperties.getRegion(),
                ossProperties.getRoleArn(),
                stsProperties);
    }

}
